package com.lf.yapinmall.product.controller;

import com.lf.yapinmall.common.utils.R;
import com.lf.yapinmall.product.entity.BrandEntity;
import com.lf.yapinmall.product.entity.CategoryBrandRelationEntity;
import com.lf.yapinmall.product.entity.CategoryEntity;
import com.lf.yapinmall.product.service.BrandService;
import com.lf.yapinmall.product.service.CategoryBrandRelationService;
import com.lf.yapinmall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * 品牌分类关联
 *
 * @author liufan
 * @email liufan@gmail.com
 * @date 2020-04-29 20:46:17
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/category/list")
    public R list(@RequestParam("brandId") Long brandId) {
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(brandId);

        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {

        CategoryBrandRelationEntity categoryBrandRelationEntity = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelationEntity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelationEntity) {
        if(categoryBrandRelationService.saveDetails(categoryBrandRelationEntity) == 0){
            return R.error("已是关联关系！");
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelationEntity) {
        categoryBrandRelationService.updateById(categoryBrandRelationEntity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
